<template>
  <div class="app-container">
    <el-card class="headerTop">
      <el-form :model="queryParams" :inline="true" label-width="68px">
        <el-form-item label="提交时间">
          <el-date-picker
            v-model="datePickerValue"
            :picker-options="pickerOptions"
            type="datetimerange"
            :editable="false"
            :clearable="false"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :default-time="['00:00:00', '23:59:59']"
            @change="selectDate"
          >
          </el-date-picker>
        </el-form-item>
      </el-form>
    </el-card>

    <el-card>
      <el-row class="mb8">
        <el-button
          v-for="(item, index) in getDateList"
          :key="index"
          size="mini"
          :class="{ isActive: isNormal === index }"
          @click="getDateFn(index)"
        >
          {{ item.name }} ({{ item.total }})
        </el-button>
      </el-row>
      <div class="main">
        <el-card
          v-for="(item, index) in discountList"
          :key="index"
          class="items"
          :class="[
            {
              border3:
                item.yiyaAuditDiscount.auditerOneResult == 2 ||
                item.yiyaAuditDiscount.auditerTwoResult == 2,
            },
            {
              border4:
                item.yiyaAuditDiscount.auditerOneResult == 3 &&
                item.yiyaAuditDiscount.auditerTwoResult == 3,
            },
          ]"
        >
          <div
            v-if="
              item.yiyaAuditDiscount.auditerOneResult == 2 ||
              item.yiyaAuditDiscount.auditerTwoResult == 2
            "
            class="positionDiv"
          >
            <img src="@/assets/image/red.png" alt="" />
          </div>
          <div
            v-if="
              item.yiyaAuditDiscount.auditerOneResult == 3 &&
              item.yiyaAuditDiscount.auditerTwoResult == 3
            "
            class="positionDiv"
          >
            <img src="@/assets/image/green.png" alt="" />
          </div>
          <div class="div1">
            <div>
              <span class="fonts1">申请门店:</span>
              <span class="fonts2">{{ item.yiyaBillingMain.clinic }}</span>
            </div>
            <div>
              <span class="fonts1">申请TC:</span>
              <span class="fonts2">{{ item.yiyaBillingMain.tc }}</span>
            </div>
            <div>
              <span class="fonts1">申请时间:</span>
              <span class="fonts2">{{ item.yiyaBillingMain.createTime }}</span>
            </div>
          </div>
          <div
            class="div2"
            :class="[
              {
                bgcColor1:
                  item.yiyaAuditDiscount.auditerOneResult == 2 ||
                  item.yiyaAuditDiscount.auditerTwoResult == 2,
              },
              {
                bgcColor2:
                  item.yiyaAuditDiscount.auditerOneResult == 3 &&
                  item.yiyaAuditDiscount.auditerTwoResult == 3,
              },
            ]"
          >
            <div class="width1 fonts3">申请项目</div>
            <div class="width2 fonts3">单价(元)</div>
            <div class="width3 fonts3">折扣(%)</div>
            <div class="width4 fonts3">折后价格(元)</div>
            <!-- <div class="width5 fonts3">项目定价(元)</div> -->
          </div>
          <div class="div3">
            <el-tooltip class="item" effect="dark" placement="bottom">
              <div slot="content">
                {{ item.yiyaItemBillingExtends[0].itemName }}
              </div>
              <div class="width1 fonts3 bottom4">
                {{ item.yiyaItemBillingExtends[0].itemName }}
              </div>
            </el-tooltip>
            <div class="width2 border1 fonts3">
              {{ fmtFloat(item.yiyaItemBillingExtends[0].unitPrice) }}
            </div>
            <div class="width3 border2 fonts5">
              {{
                parseInt(
                  (item.yiyaItemBillingExtends[0].projectReceivable /
                    (item.yiyaItemBillingExtends[0].unitPrice *
                      item.yiyaItemBillingExtends[0].count)) *
                    100
                )
              }}%
            </div>
            <div class="width4 fonts5">
              {{ fmtFloat(item.yiyaItemBillingExtends[0].projectReceivable) }}
            </div>
            <!-- <div class="width5 fonts4">
              {{
                fmtFloat(item.yiyaItemBillingExtends[0].projectReceivable)
              }}~{{ fmtFloat(item.yiyaItemBillingExtends[0].unitPrice) }}
            </div> -->
          </div>
          <div class="div4">
            <div
              class="leftDiv"
              :class="{ border2: item.yiyaAuditDiscount.auditerTwo }"
              :style="{
                width: item.yiyaAuditDiscount.auditerTwo ? '50%' : '100%',
              }"
            >
              <div>
                <span class="fonts1">审核人:</span>
                <span class="fonts2">{{
                  item.yiyaAuditDiscount.auditerOne
                }}</span>
              </div>
              <div class="margin1">
                <span class="fonts1">审核时间:</span>
                <span class="fonts2">{{
                  item.yiyaAuditDiscount.auditerOneTime
                }}</span>
              </div>
              <div
                v-if="item.yiyaAuditDiscount.auditerOneReason"
                class="margin1"
              >
                <el-tooltip class="item" effect="dark" placement="bottom">
                  <div slot="content">
                    {{ item.yiyaAuditDiscount.auditerOneReason }}
                  </div>
                  <div class="bottom2">
                    {{ item.yiyaAuditDiscount.auditerOneReason }}
                  </div>
                </el-tooltip>
              </div>
              <div
                v-if="
                  veriLevel == 1 && item.yiyaAuditDiscount.auditerOneResult == 1
                "
                class="imgBox"
              >
                <el-button type="primary" @click="goDetails(item)"
                  >去审核<i class="el-icon-d-arrow-right el-icon--right"></i
                ></el-button>
              </div>
              <div
                v-if="item.yiyaAuditDiscount.auditerOneResult == 2"
                class="imgBox"
              >
                <img src="@/assets/image/no.png" alt="" />
              </div>
              <div
                v-if="item.yiyaAuditDiscount.auditerOneResult == 3"
                class="imgBox"
              >
                <img src="@/assets/image/yes.png" alt="" />
              </div>
            </div>
            <div v-if="item.yiyaAuditDiscount.auditerTwo" class="rightDiv">
              <div>
                <span class="fonts1">审核人:</span>
                <span class="fonts2">{{
                  item.yiyaAuditDiscount.auditerTwo
                }}</span>
              </div>
              <div class="margin1">
                <span class="fonts1">审核时间:</span>
                <span class="fonts2">{{
                  item.yiyaAuditDiscount.auditerTwoTime
                }}</span>
              </div>
              <div
                v-if="item.yiyaAuditDiscount.auditerTwoReason"
                class="margin1"
              >
                <el-tooltip class="item" effect="dark" placement="bottom">
                  <div slot="content">
                    {{ item.yiyaAuditDiscount.auditerTwoReason }}
                  </div>
                  <div class="bottom2">
                    {{ item.yiyaAuditDiscount.auditerTwoReason }}
                  </div>
                </el-tooltip>
              </div>
              <div
                v-if="
                  veriLevel == 2 && item.yiyaAuditDiscount.auditerTwoResult == 1
                "
                class="imgBox"
              >
                <el-button type="primary" @click="goDetails(item)"
                  >去审核<i class="el-icon-d-arrow-right el-icon--right"></i
                ></el-button>
              </div>
              <div
                v-if="item.yiyaAuditDiscount.auditerTwoResult == 2"
                class="imgBox"
              >
                <img src="@/assets/image/no.png" alt="" />
              </div>
              <div
                v-if="item.yiyaAuditDiscount.auditerTwoResult == 3"
                class="imgBox"
              >
                <img src="@/assets/image/yes.png" alt="" />
              </div>
            </div>
          </div>
        </el-card>
      </div>
    </el-card>
  </div>
</template>

<script>
import { listDiscounts } from '@/api/erp/discount'
import { dateMap } from '@/common/date'
import { getTodayStartWithEnd } from '@/common/getDate'
import { mapState } from 'vuex'
import { encode } from '@/utils/utils'

export default {
  name: 'Discount',
  data() {
    return {
      getDateList: [
        { name: '待我审核', total: 0 },
        { name: '我已审核', total: 0 },
      ],
      pickerOptions: {
        shortcuts: [
          {
            text: '今天',
            onClick(picker) {
              picker.$emit('pick', dateMap['今日'])
            },
          },
          {
            text: '昨天',
            onClick(picker) {
              picker.$emit('pick', dateMap['昨日'])
            },
          },
          {
            text: '本周',
            onClick(picker) {
              picker.$emit('pick', dateMap['本周'])
            },
          },
          {
            text: '本月',
            onClick(picker) {
              picker.$emit('pick', dateMap['本月'])
            },
          },
          {
            text: '同比',
            onClick(picker) {
              picker.$emit('pick', dateMap['同比'])
            },
          },
        ],
      },
      datePickerValue: [],
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 折扣审核表格数据
      discountList: [],
      // 查询参数
      queryParams: {
        beginTime: undefined,
        endTime: undefined,
      },
      // 表单参数
      form: {},
      isNormal: 0,
      datePickerKey: '今日',
    }
  },
  computed: {
    ...mapState({
      veriLevel: (state) => state.user.veriLevel,
      userName: (state) => state.user.name,
    }),
  },
  watch: {
    datePickerKey: {
      handler() {
        this.handleDateButtonKeyChange()
      },
      immediate: true,
    },
  },
  methods: {
    goDetails(row) {
      this.$router.push(
        `/audit/details?id=${
          row.yiyaBillingMain.clinicPersonId
        }&redirect=${encode({
          path: this.$route.path,
          query: row,
        })}`
      )
    },
    handleDateButtonKeyChange() {
      this.datePickerValue = getTodayStartWithEnd()
      this.changeTime(this.datePickerValue)
    },
    changeTime(e) {
      this.queryParams.beginTime = e[0].$d
      this.queryParams.endTime = e[1].$d
      this.getList()
    },
    getDateFn(index) {
      this.isNormal = index
      this.getList()
    },
    selectDate(e) {
      if (e == null) {
        this.getList()
      } else {
        if (e[0].$d !== undefined) {
          this.queryParams.beginTime = e[0].$d
          this.queryParams.endTime = e[1].$d
        } else {
          this.queryParams.beginTime = e[0]
          this.queryParams.endTime = e[1]
        }
        this.getList()
      }
    },
    /** 查询折扣审核列表 */
    getList() {
      this.loading = true
      // if(this.veriLevel == 1) {
      //   if(this.isNormal == 0) {
      //     this.queryParams = {
      //       ...this.queryParams,
      //       auditerOne: this.userName,
      //       auditerOneResult: 1
      //     }
      //   }else {
      //     this.queryParams = {
      //       ...this.queryParams,
      //       auditerOne: this.userName,
      //       auditerOneResult: 1
      //     }
      //   }
      // }else if(this.veriLevel == 2){
      //   this.queryParams = {
      //     ...this.queryParams,
      //     auditerTwo: this.userName
      //   }
      // }else {
      //   return
      // }
      listDiscounts(this.queryParams).then((response) => {
        if (this.veriLevel == 1) {
          //一级审核人
          if (this.isNormal == 0) {
            // 待我审核
            this.discountList = response.rows.filter(
              (item) =>
                item.yiyaAuditDiscount.auditerOne == this.userName &&
                item.yiyaAuditDiscount.auditerOneResult == 1
            )
            const arr = response.rows.filter(
              (item) =>
                item.yiyaAuditDiscount.auditerOne == this.userName &&
                item.yiyaAuditDiscount.auditerOneResult !== 1
            )
            this.getDateList[0].total = this.discountList.length
            this.getDateList[1].total = arr.length
            this.discountList = this.discountList.reverse()
          } else {
            //我已审核
            this.discountList = response.rows.filter(
              (item) =>
                item.yiyaAuditDiscount.auditerOne == this.userName &&
                item.yiyaAuditDiscount.auditerOneResult !== 1
            )
            const arr = response.rows.filter(
              (item) =>
                item.yiyaAuditDiscount.auditerOne == this.userName &&
                item.yiyaAuditDiscount.auditerOneResult == 1
            )
            this.getDateList[1].total = this.discountList.length
            this.getDateList[0].total = arr.length
            this.discountList = this.discountList.reverse()
          }
        } else if (this.veriLevel == 2) {
          //二级审核人
          if (this.isNormal == 0) {
            // 待我审核
            this.discountList = response.rows.filter(
              (item) =>
                item.yiyaAuditDiscount.auditerTwo == this.userName &&
                item.yiyaAuditDiscount.auditerTwoResult == 1
            )
            const arr = response.rows.filter(
              (item) =>
                item.yiyaAuditDiscount.auditerTwo == this.userName &&
                item.yiyaAuditDiscount.auditerTwoResult !== 1
            )
            this.getDateList[0].total = this.discountList.length
            this.getDateList[1].total = arr.length
            this.discountList = this.discountList.reverse()
          } else {
            //我已审核
            this.discountList = response.rows.filter(
              (item) =>
                item.yiyaAuditDiscount.auditerTwo == this.userName &&
                item.yiyaAuditDiscount.auditerTwoResult !== 1
            )
            const arr = response.rows.filter(
              (item) =>
                item.yiyaAuditDiscount.auditerTwo == this.userName &&
                item.yiyaAuditDiscount.auditerTwoResult == 1
            )
            this.getDateList[1].total = this.discountList.length
            this.getDateList[0].total = arr.length
            this.discountList = this.discountList.reverse()
          }
        } else {
          this.discountList = []
          this.getDateList[0].total = 0
          this.getDateList[1].total = 0
        }
        this.total = response.total
      })
      this.loading = false
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        billingMainId: undefined,
        clinic: undefined,
        documentNum: undefined,
        proposer: undefined,
        proposerTime: undefined,
        auditerOne: undefined,
        auditerOneTime: undefined,
        auditerOneResult: undefined,
        auditerOneReason: undefined,
        auditerTwo: undefined,
        auditerTwoTime: undefined,
        auditerTwoResult: undefined,
        auditerTwoReason: undefined,
        status: '0',
        remarks: undefined,
        createTime: undefined,
        createBy: undefined,
        updateTime: undefined,
        updateBy: undefined,
      }
      this.resetForm('form')
    },
  },
}
</script>

<style lang="scss" scoped>
.headerTop {
  margin-bottom: 18px;
}
.isActive {
  background: #1890ff !important;
  border-color: #1890ff !important;
  color: #fff !important;
}
.headerTop >>> .el-card__body {
  padding: 20px 20px 0px 20px;
}
.main {
  display: flex;
  justify-content: space-between;
  margin-top: 20px;
  flex-wrap: wrap;
  .items {
    width: 49%;
    display: flex;
    flex-direction: column;
    margin-bottom: 20px;
    position: relative;
    .positionDiv {
      display: flex;
      position: absolute;
      height: 22px;
      width: 14px;
      top: 0;
      img {
        flex-shrink: 0;
        height: 100%;
        width: 100%;
      }
    }
    .div1 {
      display: flex;
      flex-wrap: nowrap;
      justify-content: space-around;
      align-items: center;
      height: 60px;
    }
    .div2 {
      background-color: #e2eeff;
      display: flex;
      flex-wrap: nowrap;
      align-items: center;
      height: 60px;
      div {
        font-weight: 510;
        color: #4f5e7b;
        text-align: center;
      }
    }
    .div3 {
      display: flex;
      flex-wrap: nowrap;
      align-items: center;
      height: 60px;
      border-bottom: 2px solid #ccc;
      div {
        height: 60px;
        line-height: 60px;
        text-align: center;
      }
    }
    .div4 {
      display: flex;
      justify-content: space-between;
      flex-wrap: nowrap;
      .leftDiv,
      .rightDiv {
        width: 50%;
        display: flex;
        flex-direction: column;
        position: relative;
        height: 128px;
        padding-left: 25px;
        padding-top: 20px;
        .imgBox {
          position: absolute;
          bottom: 0;
          right: 20px;
          img {
            flex: 0;
          }
        }
      }
    }
  }
}
.fonts1 {
  color: #4f5e7b;
  font-weight: 520;
}
.fonts2 {
  color: #222;
  font-weight: 550;
}
.fonts3 {
  font-weight: 500;
}
.fonts4 {
  font-weight: 600;
}
.fonts5 {
  font-weight: 500;
  color: #f76359;
}
.width1 {
  width: 36.5%;
}
.width2 {
  width: 21.5%;
}
.width3 {
  width: 18.5%;
}
.width4 {
  width: 23.5%;
}
.width5 {
  width: 26%;
}
.border1 {
  border-left: 1px solid #ccc;
  border-right: 1px solid #ccc;
}
.border2 {
  border-right: 1px solid #ccc;
}
.margin1 {
  margin: 10px 0;
}
.selectMore {
  line-height: 30px;
}
.bottom2 {
  font-size: 20px;
  width: 100%;
  color: #f76;
  justify-content: center;
  box-sizing: border-box;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: normal;
  word-break: break-all;
  white-space: nowrap;
}
.bottom4 {
  justify-content: center;
  box-sizing: border-box;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: normal;
  word-break: break-all;
  white-space: nowrap;
}
.widths {
  width: 100%;
}
.border3 {
  border: 2px solid #f76359;
}
.bgcColor1 {
  background-color: #feeceb !important;
}
.bgcColor2 {
  background-color: #dcfcff !important;
}
.border4 {
  border: 2px solid #37c1ce;
}
</style>
